package com.gqp.view;


import com.gqp.engine.UnCompress;
import com.gqp.utils.GUIUtil;
import com.gqp.utils.StringConstant;
import com.gqp.utils.Utils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * gqp 操作
 */
public class CompressFrame extends JFrame implements ActionListener {
    /************************定义各控件******************************/


    private JButton unkeyword = new JButton("需要解压的压缩文件所在位置：");
    private JTextField unkeyAccount = new JTextField(10);
    private JButton unkeyword1 = new JButton("需要解压的压缩文件解压后位置：");
    private JTextField unkeyAccount1 = new JTextField(10);


    private JButton onkeyword = new JButton("需要压缩文件的所在位置：");
    private JTextField onkeyAccount = new JTextField(10);
    private JButton onkeyword1 = new JButton("需要压缩文件压缩后位置：");
    private JTextField onkeyAccount1 = new JTextField(10);

    private JButton btQuery = new JButton("确定");
    private JButton btExit = new JButton("退出");
    private String param = null;

    public CompressFrame(String param) {
        /****************************界面初始化************************/
        super("解压缩工具");
        this.param = param;
        this.setLayout(new GridLayout(3, 0));
        switch (param) {
            case StringConstant.COMPRESSION:
                this.add(onkeyword);
                this.add(onkeyAccount);
                this.add(onkeyword1);
                this.add(onkeyAccount1);
                break;
            case StringConstant.DECOMPRESSION:
                this.add(unkeyword);
                this.add(unkeyAccount);
                this.add(unkeyword1);
                this.add(unkeyAccount1);
                break;
        }
        this.add(btQuery);
        this.add(btExit);
        this.setSize(500, 250);
        GUIUtil.toCenter(this);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setVisible(true);

        /**********************增加监听*******************************/
        btQuery.addActionListener(this);
        btExit.addActionListener(this);
        unkeyword.addActionListener(this);
        unkeyword1.addActionListener(this);
        unkeyAccount.addActionListener(this);
        unkeyAccount1.addActionListener(this);

        onkeyword.addActionListener(this);
        onkeyword1.addActionListener(this);
        onkeyAccount.addActionListener(this);
        onkeyAccount1.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {

            boolean unCompressFlag = false;
            if (e.getSource() == onkeyword || e.getSource() == onkeyword1 || e.getSource() == unkeyword || e.getSource() == unkeyword1) {
                switch (this.param) {
                    case StringConstant.COMPRESSION:// 压缩
                        if (e.getSource() == onkeyword) {
                            this.onkeyAccount.setText(Utils.getUploadPath(this));
                        }
                        if (e.getSource() == onkeyword1) {
                            this.onkeyAccount1.setText(Utils.getDownloadPath(this));
                        }
                        if (!Utils.checkOnValue(this.onkeyAccount.getText(), this.onkeyAccount1.getText(), this)) {
                            return;
                        } else {

                        }
                        break;
                    case StringConstant.DECOMPRESSION:// 解压
                        if (e.getSource() == unkeyword) {
                            this.unkeyAccount.setText(Utils.getUploadPath(this));
                        }
                        if (e.getSource() == unkeyword1) {
                            this.unkeyAccount1.setText(Utils.getDownloadPath(this));
                        }
                        if (!Utils.checkUnValue(this.unkeyAccount.getText(), this.unkeyAccount1.getText(), this)) {
                            return;
                        }
                        break;
                }
            }

            if (e.getSource() == btQuery) {
                UnCompress unCompress =  Utils.getUnCompress(this.unkeyAccount.getText(), this.unkeyAccount.getText(), this.unkeyAccount1.getText());
                if (unCompress == null) {
                    JOptionPane.showMessageDialog(this, "暂不支持解压此类型");
                }else {
                    unCompress.unCompress();
                }
            }

            if (e.getSource() == btExit) {
                JOptionPane.showMessageDialog(this, "欢迎再次使用");
                System.exit(0);
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}
